package cn.hdax.test1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class StuMr implements Tool {

    private Configuration configuration ;

    public static void main(String[] args) throws Exception{
        int result = ToolRunner.run(new StuMr(),args) ;
        System.exit(result);
    }

    /**
     * 配置作业
     * @param args
     * @return
     * @throws Exception
     */
    public int run(String[] args) throws Exception {
        //声明作业
        Job job = Job.getInstance(this.getConf(),"stumr") ;
        job.setJarByClass(StuMr.class);
        System.setProperty("HADOOP_USER_NAME", "root");
        //作业的第一部份
        job.setMapperClass(StuMapper.class);
        //定义mapper的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //定义mapper的数据源
        FileInputFormat.setInputPaths(job,new Path("/stu.txt"));
        Path pathout = new Path("/t176/stu/out") ;
        FileSystem fileSystem = FileSystem.get(configuration) ;
        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true) ;
        }
        //定义mapper的输出位置
        FileOutputFormat.setOutputPath(job,pathout);

        TableMapReduceUtil.initTableReducerJob("stu1", StuReduce.class, job,null,null,null,null,false);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Put.class);


        //提交作业
        boolean flag = job.waitForCompletion(true) ;
        return flag ? 0 :1;
    }

    public static class StuMapper extends Mapper<LongWritable,Text,Text,NullWritable>
    {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            context.write(value,NullWritable.get());
        }
    }

    public static class StuReduce extends TableReducer<Text, NullWritable, NullWritable> {

        @Override
        protected void reduce(Text key, Iterable<NullWritable> values,Context context)
                throws IOException, InterruptedException {

            String[] split = key.toString().split(",");

            Put put = new Put(split[0].getBytes());

            put.addColumn("info".getBytes(), "name".getBytes(), split[1].getBytes());
            put.addColumn("info".getBytes(), "sex".getBytes(), split[2].getBytes());
            put.addColumn("info".getBytes(), "age".getBytes(), split[3].getBytes());
            put.addColumn("info".getBytes(), "department".getBytes(), split[4].getBytes());

            context.write(NullWritable.get(), put);

        }

    }

    @Override
    public void setConf(Configuration conf) {
        this.configuration = HBaseConfiguration.create() ;
    }

    @Override
    public Configuration getConf() {
        return this.configuration;
    }
}
